<?php
namespace Home\Controller;
use Think\Controller;
class CheckCompanyController extends Controller {

	public function allstep()
	{
		$this->step1();
		$this->step2();
		$this->step3();
	}

	//找出识别号或名字不一致的
	public function step1()
	{
		$md_CzjCom=D("CzjCom");
    	$md_comlist=D("ComList");
    	$all_com_rows=$md_CzjCom->getAllRows();
    	foreach ($all_com_rows as $cominfo) 
		{
			$nsrsbh=$cominfo['com_nsrsbh'];
			$nsrmc=$cominfo['com_name'];
			$now_comid=$cominfo['comid'];

			$csv_row=$md_comlist->find_row($nsrsbh,$nsrmc);
			if(empty($csv_row))
			{
				addnewLog("企业:$nsrmc 没有在csv数据中找到完全一致的数据;");
				//echo "企业:$nsrmc 没有在csv数据中找到完全一致的数据;\r\n";
				//2.2.按识别号单独查询
				$csv_row2=$md_comlist->find_row($nsrsbh,'');
				if(empty($csv_row2))
				{
					addnewLog("识别号:$nsrsbh 没有在csv数据中找到一致的数据;");
					//echo "识别号:$nsrsbh 没有在csv数据中找到一致的数据;\r\n";
					//2.3.按企业名称单独查询
					$csv_row3=$md_comlist->find_row('',$nsrmc);
					if(empty($csv_row3))
					{
						addnewLog("企业名称:$nsrmc 没有在csv数据中找到一致的数据;");
					}
					else
					{
						addnewLog("企业:$nsrmc 识别号:[$nsrsbh]和CSV数据中的[".$csv_row3['nsrsbh']."]不一致;");
						//名字对上.识别号对不上.更新识别号
						$md_CzjCom->upComSbh($now_comid,$csv_row3['nsrsbh']);
					}
				}
				else
				{
					if($nsrmc!=$csv_row2['nsrmc'])
					{
						addnewLog("识别号:[$nsrsbh]的企业名称:[$nsrmc]和CSV数据中的[".$csv_row2['nsrmc']."]不一致;");
						//echo "识别号:[$nsrsbh]的企业名称:[$nsrmc]和CSV数据中的[".$csv_row2['nsrmc']."]不一致;\r\n";
						//更新数据中的企业名称
						$md_CzjCom->upComName($now_comid,$csv_row2['nsrmc']);
					}
					else
					{
						addnewLog("识别号:[$nsrsbh]的企业名称:[$nsrmc]和CSV数据中的[".$csv_row2['nsrmc']."]一致;但MD5值不一致;");
					}
					//$company_states=false;
				}
			}
    	}
	}

	//找出识别号重复的.合并掉
	public function step2()
	{
		$sql="SELECT *
				FROM (SELECT com_nsrsbh, COUNT(comid) AS ts
				        FROM czj_com
				        GROUP BY com_nsrsbh) table_b
				WHERE (ts > 1)";
		$md_CzjCom=D("CzjCom");
		$rows = $md_CzjCom->query($sql);
		foreach($rows as $row)
		{
			$repeat_rows=$md_CzjCom->getAllRows("com_nsrsbh='".$row['com_nsrsbh']."'");
			$i=0;
			$use_com_id=0;

			foreach($repeat_rows as $repeat_row)
			{
				$i++;
				if($i==1)
				{
					$use_com_id=$repeat_row['comid'];
					continue;
				}
				

				$md_CzjCom->mergeCompany($repeat_row['comid'],$use_com_id);
			}
			
		}
	}
	//找出名字重复的.合并掉
	public function step3()
	{
		$sql="SELECT *
				FROM (SELECT com_name, COUNT(comid) AS ts
				        FROM czj_com
				        GROUP BY com_name) table_b
				WHERE (ts > 1)";
		$md_CzjCom=D("CzjCom");
		$rows = $md_CzjCom->query($sql);
		foreach($rows as $row)
		{
			$repeat_rows=$md_CzjCom->getAllRows("com_name='".$row['com_name']."'");
			$i=0;
			$use_com_id=0;

			foreach($repeat_rows as $repeat_row)
			{
				$i++;
				if($i==1)
				{
					$use_com_id=$repeat_row['comid'];
					continue;
				}
				

				$md_CzjCom->mergeCompany($repeat_row['comid'],$use_com_id);
			}
			
		}
	}



	

}